import { Component, OnInit } from '@angular/core';
import { NzModalRef, NzMessageService } from 'ng-zorro-antd';

import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { UserService } from 'src/app/core/service/user.service';

@Component({
  selector: 'app-add-group-user',
  templateUrl: './add-group-user.component.html',
  styleUrls: ['./add-group-user.component.scss']
})
export class AddGroupUserComponent implements OnInit {
  groupUserForm: FormGroup;
  radioValue = '0';
  isAllocation = true;
  defaultValue = 0;
  info = {};

  msgFlag = '';
  nameFlag = '';

  constructor(
    private modal: NzModalRef,
    private message: NzMessageService,
    private userService: UserService,
    fb: FormBuilder
  ) {
    this.groupUserForm = fb.group({
      groupName: [null, Validators.compose([Validators.required])],
      autoScaLicense: [null, Validators.compose([Validators.required])],
      assignLicenseNum: [0, Validators.compose([Validators.required])],
      email: [null, Validators.compose([Validators.required, Validators.email])],
      lastName: [null, Validators.compose([Validators.required])],
      firstName: [null, Validators.compose([Validators.required])]
    });
  }

  /**
   * 显示数字输入框
   * @param value 传过来的值
   */
  log(value: string[]): void {
    if (value.length > 0) {
      this.isAllocation = false;
    } else {
      this.isAllocation = true;
    }
  }

  saveGroupUser() {
    console.log('提交', this.groupUserForm.controls.assignLicenseNum.valid);
    // tslint:disable-next-line:forin
    for (const c in this.groupUserForm.controls) {
      this.groupUserForm.controls[c].markAsTouched();
    }

    if (this.groupUserForm.valid) {
      this.info = {
        'groupName': this.groupUserForm.controls.groupName.value,
        'autoScaLicense': this.groupUserForm.controls.autoScaLicense.value,
        'assignLicenseNum': this.groupUserForm.controls.assignLicenseNum.value,
        'email': this.groupUserForm.controls.email.value,
        'firstName': this.groupUserForm.controls.firstName.value,
        'lastName': this.groupUserForm.controls.lastName.value
      };
      this.addService(this.info);
    } else {
      this.message.error('表单校验失败', { nzDuration: 10000 });
    }
  }

  private addService(info: any) {
    this.userService.saveGroupUser(this.info)
      .subscribe((saveGroupUser) => {
        if (saveGroupUser.status) {
          this.message.success('添加成功!', {
            nzDuration: 10000
          });
          this.modal.destroy();
        } else {
          this.message.error('添加失败!请检查后重新填写', {
            nzDuration: 10000
          });
        }
      });
  }

  destroyModal(): void {
    this.modal.destroy({ data: 'this the result data' });
  }

  /**
   * 判断小组名称是否重复
   */
  isGroupNameRepeat() {
    console.log('=== Janine.isOrgNameRepeat().groupName ===>', this.groupUserForm.controls.groupName.value);
    if (this.groupUserForm.controls.groupName.value) {
      this.userService.isGroupNameRepeat(this.groupUserForm.controls.groupName.value)
        .subscribe((data) => {
          console.log(data);
          if (data.status === 3) { // 重复
            this.nameFlag = 'error';
          }
          if (data.status === 1) { // 可用
            this.nameFlag = 'success';
          }
        });
    }
  }

  /**
   * 判断邮箱是否重复
   */
  isEmailRepeat() {
    console.log('=== Janine.isEmailRepeat().email ===>', this.groupUserForm.controls.email.value);
    if (this.groupUserForm.controls.email.value) {
      this.userService.isEmailRepeat(this.groupUserForm.controls.email.value)
        .subscribe((data) => {
          console.log(data);
          if (data.status === 3) { // 重复
            this.msgFlag = 'error';
          }
          if (data.status === 1) { // 可用
            this.msgFlag = 'success';
          }
        });
    }
  }

  ngOnInit() {
  }

}
